package com.red.basic.service.impl;

import java.util.List;

import com.red.basic.domain.RedCustomFolder;
import com.red.basic.dto.RedCustomDto;
import com.red.basic.service.IRedCustomFolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.red.basic.mapper.RedCustomMapper;
import com.red.basic.domain.RedCustom;
import com.red.basic.service.IRedCustomService;

/**
 * 客户管理Service业务层处理
 * 
 * @author bug2222
 * @date 2024-11-09
 */
@Service
public class RedCustomServiceImpl implements IRedCustomService 
{
    @Autowired
    private RedCustomMapper redCustomMapper;
    @Autowired
    private IRedCustomFolderService folderService;

    /**
     * 查询客户管理
     * 
     * @param id 客户管理主键
     * @return 客户管理
     */
    @Override
    public RedCustom selectRedCustomById(Long id)
    {
        return redCustomMapper.selectRedCustomById(id);
    }

    /**
     * 查询客户管理列表
     * 
     * @param redCustom 客户管理
     * @return 客户管理
     */
    @Override
    public List<RedCustomDto> selectRedCustomList(RedCustomDto redCustom)
    {
        return redCustomMapper.selectRedCustomList(redCustom);
    }

    /**
     * 新增客户管理
     * 
     * @param redCustom 客户管理
     * @return 结果
     */
    @Override
    public int insertRedCustom(RedCustomDto redCustom)
    {
        return redCustomMapper.insertRedCustom(redCustom);
    }

    /**
     * 修改客户管理
     * 
     * @param redCustom 客户管理
     * @return 结果
     */
    @Override
    public int updateRedCustom(RedCustomDto redCustom)
    {
        return redCustomMapper.updateRedCustom(redCustom);
    }

    /**
     * 批量删除客户管理
     * 
     * @param ids 需要删除的客户管理主键
     * @return 结果
     */
    @Override
    public int deleteRedCustomByIds(Long[] ids)
    {
        return redCustomMapper.deleteRedCustomByIds(ids);
    }

    /**
     * 删除客户管理信息
     * 
     * @param id 客户管理主键
     * @return 结果
     */
    @Override
    public int deleteRedCustomById(Long id)
    {
        return redCustomMapper.deleteRedCustomById(id);
    }

    @Override
    public List<RedCustomFolder> getCustomFolder() {
        List<RedCustomFolder> redCustomFolder = folderService.getRootFolder();
        setChildren(redCustomFolder);
        return redCustomFolder;
    }

    private void setChildren(List<?> redCustomFolderList) {
        redCustomFolderList.forEach(item -> {
            if (item instanceof RedCustomFolder) {
                RedCustomFolder folder = (RedCustomFolder) item;
                RedCustomFolder param = new RedCustomFolder();
                param.setParentId(folder.getId());
                folder.setChildren(folderService.selectRedCustomFolderList(param));
                setChildren(folder.getChildren());
            }

        });
    }
}
